package com.eastmoney.emis.edda.config;

import com.eastmoney.emis.edda.infrastructure.actuate.PGPCertificateHealthIndicator;
import com.eastmoney.emis.edda.infrastructure.actuate.PGPCertificateHealthIndicatorProperties;
import com.eastmoney.emis.edda.infrastructure.encrypt.Encryption;
import com.eastmoney.emis.edda.infrastructure.hsbc.converter.HsbcEddaEncryptMessageConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: Description
 * @projectName: EDDA-HSBC
 * @package: com.eastmoney.emis.edda.config
 * @author: 汪程程
 * @date: 2022/9/26 5:08 PM
 * @version: 1.0
 */
@Configuration
public class AppConfig {

    @Bean("hsbcEddaRestTemplate")
    public RestTemplate hsbcEddaRestTemplate(RestTemplate restTemplate, HsbcEddaConfig hsbcEddaConfig, Encryption encryption) {
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        messageConverters.add(new HsbcEddaEncryptMessageConverter(encryption, hsbcEddaConfig));
        messageConverters.addAll(restTemplate.getMessageConverters());
        restTemplate.setMessageConverters(messageConverters);
        return restTemplate;
    }


    @EnableConfigurationProperties({PGPCertificateHealthIndicatorProperties.class})
    public static class PGPCertificateHealthConfig {

        @Bean
        @ConditionalOnProperty(prefix = "edda.pgp.health", name = "enable", havingValue = "true")
        public PGPCertificateHealthIndicator pgpCertificateHealthIndicator(PGPCertificateHealthIndicatorProperties properties, KeyHolder keyHolder) {
            return new PGPCertificateHealthIndicator(keyHolder, properties);
        }
    }


}
